package com.qf.contller;

import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lyx
 * @date 2022/7/6 15:54
 */
@RestController
@RequestMapping("/test")
public class TestContller {
    @Autowired
    private EurekaClient eurekaClient;
    /**
     * restTemplate是Spring封装的java服务器发送http协议的远程工具
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SearchFeign searchFeign;
    /**
     * 测试远程调用底层原理
     */
    @RequestMapping("/consumer")
    public String consumer(){
        //1.通过eurekaClient对象获取search搜索服务对象信息
//        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
//        //2.从搜索服务对象信息中获取搜索服务的ip和端口号
//        String url = info.getHomePageUrl();
//        System.out.println("url"+url);
        //3.通过restTenplate对象发送http协议远程调用
        String str = restTemplate.getForObject("http://QF-SEARCH/search/test", String.class);
        //4.返回结果
        System.out.println("str"+str);
        return str;


    }
/**
 * 测试feign调用接口
 */
@RequestMapping("/testFeign")
    public String testFeign(){
    String str = searchFeign.test1();
    System.out.println(str);
    return str;
}

/**
 * 测试远程调用传参
 */
@RequestMapping("/findUserId")
public User testfindUserId() {
    User user = searchFeign.findUserId(1);
    System.out.println(user);
    return user;
}
}
